
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>路由元信息 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="transitions.html" />
    
    
    <link rel="prev" href="navigation-guards.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../installation.html">
            
                <a href="../installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../essentials/getting-started.html">
            
                <a href="../essentials/getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../essentials/dynamic-matching.html">
            
                <a href="../essentials/dynamic-matching.html">
            
                    
                    动态路由匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../essentials/nested-routes.html">
            
                <a href="../essentials/nested-routes.html">
            
                    
                    嵌套路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../essentials/navigation.html">
            
                <a href="../essentials/navigation.html">
            
                    
                    编程式导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../essentials/named-routes.html">
            
                <a href="../essentials/named-routes.html">
            
                    
                    命名路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../essentials/named-views.html">
            
                <a href="../essentials/named-views.html">
            
                    
                    命名视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../essentials/redirect-and-alias.html">
            
                <a href="../essentials/redirect-and-alias.html">
            
                    
                    重定向 和 别名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../essentials/history-mode.html">
            
                <a href="../essentials/history-mode.html">
            
                    
                    HTML5 History 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    进阶
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="navigation-guards.html">
            
                <a href="navigation-guards.html">
            
                    
                    导航钩子
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="meta.html">
            
                <a href="meta.html">
            
                    
                    路由元信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="transitions.html">
            
                <a href="transitions.html">
            
                    
                    过渡动效
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="data-fetching.html">
            
                <a href="data-fetching.html">
            
                    
                    数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="scroll-behavior.html">
            
                <a href="scroll-behavior.html">
            
                    
                    滚动行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="lazy-loading.html">
            
                <a href="lazy-loading.html">
            
                    
                    懒加载
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    API 文档
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../api/router-link.html">
            
                <a href="../api/router-link.html">
            
                    
                    router-link
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../api/router-view.html">
            
                <a href="../api/router-view.html">
            
                    
                    router-view
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../api/route-object.html">
            
                <a href="../api/route-object.html">
            
                    
                    路由信息对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../api/options.html">
            
                <a href="../api/options.html">
            
                    
                    Router 构造配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../api/router-instance.html">
            
                <a href="../api/router-instance.html">
            
                    
                    Router 实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../api/component-injections.html">
            
                <a href="../api/component-injections.html">
            
                    
                    对组件注入
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >路由元信息</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8DEF;&#x7531;&#x5143;&#x4FE1;&#x606F;">&#x8DEF;&#x7531;&#x5143;&#x4FE1;&#x606F;</h1>
<p>&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x914D;&#x7F6E; <code>meta</code> &#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> VueRouter({
  routes: [
    {
      path: <span class="hljs-string">&apos;/foo&apos;</span>,
      component: Foo,
      children: [
        {
          path: <span class="hljs-string">&apos;bar&apos;</span>,
          component: Bar,
          <span class="hljs-comment">// a meta field</span>
          meta: { requiresAuth: <span class="hljs-literal">true</span> }
        }
      ]
    }
  ]
})
</code></pre>
<p>&#x90A3;&#x4E48;&#x5982;&#x4F55;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A; <code>meta</code> &#x5B57;&#x6BB5;&#x5462;&#xFF1F;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x79F0;&#x547C; <code>routes</code> &#x914D;&#x7F6E;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x8DEF;&#x7531;&#x5BF9;&#x8C61;&#x4E3A; <strong>&#x8DEF;&#x7531;&#x8BB0;&#x5F55;</strong>&#x3002;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;&#x53EF;&#x4EE5;&#x662F;&#x5D4C;&#x5957;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x6210;&#x529F;&#x540E;&#xFF0C;&#x4ED6;&#x53EF;&#x80FD;&#x5339;&#x914D;&#x591A;&#x4E2A;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x6839;&#x636E;&#x4E0A;&#x9762;&#x7684;&#x8DEF;&#x7531;&#x914D;&#x7F6E;&#xFF0C;<code>/foo/bar</code> &#x8FD9;&#x4E2A; URL &#x5C06;&#x4F1A;&#x5339;&#x914D;&#x7236;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;&#x4EE5;&#x53CA;&#x5B50;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x5230;&#x7684;&#x6240;&#x6709;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;&#x4F1A;&#x66B4;&#x9732;&#x4E3A; <code>$route</code> &#x5BF9;&#x8C61;&#xFF08;&#x8FD8;&#x6709;&#x5728;&#x5BFC;&#x822A;&#x94A9;&#x5B50;&#x4E2D;&#x7684; route &#x5BF9;&#x8C61;&#xFF09;&#x7684; <code>$route.matched</code> &#x6570;&#x7EC4;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x904D;&#x5386; <code>$route.matched</code> &#x6765;&#x68C0;&#x67E5;&#x8DEF;&#x7531;&#x8BB0;&#x5F55;&#x4E2D;&#x7684; <code>meta</code> &#x5B57;&#x6BB5;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x5728;&#x5168;&#x5C40;&#x5BFC;&#x822A;&#x94A9;&#x5B50;&#x4E2D;&#x68C0;&#x67E5; meta &#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-js">router.beforeEach((to, <span class="hljs-keyword">from</span>, next) =&gt; {
  <span class="hljs-keyword">if</span> (to.matched.some(record =&gt; record.meta.requiresAuth)) {
    <span class="hljs-comment">// this route requires auth, check if logged in</span>
    <span class="hljs-comment">// if not, redirect to login page.</span>
    <span class="hljs-keyword">if</span> (!auth.loggedIn()) {
      next({
        path: <span class="hljs-string">&apos;/login&apos;</span>,
        query: { redirect: to.fullPath }
      })
    } <span class="hljs-keyword">else</span> {
      next()
    }
  } <span class="hljs-keyword">else</span> {
    next() <span class="hljs-comment">// &#x786E;&#x4FDD;&#x4E00;&#x5B9A;&#x8981;&#x8C03;&#x7528; next()</span>
  }
})
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="navigation-guards.html" class="navigation navigation-prev " aria-label="Previous page: 导航钩子">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="transitions.html" class="navigation navigation-next " aria-label="Next page: 过渡动效">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"路由元信息","level":"1.4.2","depth":2,"next":{"title":"过渡动效","level":"1.4.3","depth":2,"path":"advanced/transitions.md","ref":"advanced/transitions.md","articles":[]},"previous":{"title":"导航钩子","level":"1.4.1","depth":2,"path":"advanced/navigation-guards.md","ref":"advanced/navigation-guards.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"advanced/meta.md","mtime":"2017-03-04T12:16:27.888Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T16:00:44.406Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

